package com.standardoperationapp.utils;

import android.device.DeviceManager;
import android.text.TextUtils;

import java.io.FileOutputStream;
import java.io.IOException;

public class OtgUtils {
    private static final String NODE_53_1 = "/sys/devices/soc/c170000.serial/pogo_uart";
    private static final String NODE_53_2 = "/sys/devices/virtual/Usb_switch/usbswitch/function_otg_en";
    private static final String NODE_53C_1 = "/sys/devices/soc/soc:sectrl/ugp_ctrl/gp_pogo_5v_ctrl/enable";
    private static final String NODE_53C_2 = "/sys/devices/soc/soc:sectrl/ugp_ctrl/gp_otg_en_ctrl/enable";
    private static final String NOde_53X=    "/sys/kernel/kobject_pogo_otg_status/pogo_otg_status";
    public static boolean set53GPIOEnabled(final boolean enable ) {

        try {
            String mProjectName = new DeviceManager().getSettingProperty("pwv.project");

            String nodepath_pogo5ven = new DeviceManager().getSettingProperty("persist.sys.pogopin.otg5v.en");
            FileOutputStream node_1 = null;
            FileOutputStream node_2 = null;
            FileOutputStream node_3 = null;
            byte[] open_one = new byte[]{0x31};
            byte[] close = new byte[]{0x30};
            try {
                if (TextUtils.equals(mProjectName, "SQ53Q")||TextUtils.equals(mProjectName, "SQ53Z")) { //53、53Q、53Z上电
                    node_1 = new FileOutputStream(NODE_53_1);
                    node_1.write(enable ? open_one : close);
                    node_2 = new FileOutputStream(NODE_53C_1);
                    node_2.write(enable ? open_one : close);
                }
                else if (TextUtils.equals(mProjectName, "SQ53"))  { //53 上电
                    byte[] open_two = new byte[]{0x32};
                    node_1 = new FileOutputStream(NODE_53_1);
                    node_1.write(enable ? open_one : close);
                    node_2 = new FileOutputStream(NODE_53_2);
                    node_2.write(enable ? open_two : close);
                }
                else if (!TextUtils.isEmpty(nodepath_pogo5ven)) {
                    node_1 = new FileOutputStream(nodepath_pogo5ven);

                    node_1.write(enable ? open_one : close);

                    if(TextUtils.equals(mProjectName, "SQ53X")) {
                        node_3 = new FileOutputStream(NOde_53X);
                        node_3.write(enable ? open_one : close);
                    }
                } else { //53C
                    node_1 = new FileOutputStream(NODE_53C_1);
                    node_1.write(enable ? open_one : close);
                    node_2 = new FileOutputStream(NODE_53C_2);
                    node_2.write(enable ? open_one : close);

                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                try {
                    if (node_1 != null) {
                        node_1.close();
                    }
                    if (node_2 != null) {
                        node_2.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            return false;
        }

    }




}
